<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Base Sprite</title>
  <style>
    sprite.test {
      border: solid 10px red;
      border-radius: 25px 10px;
    }
  </style>
</head>
<body>
  <canvas id="mycanvas" width="600" height="600"></canvas>
  <script src="/js/sprite-core.js"></script>
  <script>
    const Sprite = spritejs.Sprite;
    const Layer = spritejs.Layer;
    const Group = spritejs.Group;
    const context = document.getElementById('mycanvas').getContext('2d');
    const layer = new Layer({context, useDocumentCSS: true});

    const s = new Group({
      id: 'abc',
      // padding: [50, 50, 50, 50],
      // bgcolor: 'red',
      // anchor: 0.5,
      // bgcolor: 'red',
      // size: [500, 500],
      // pos: [300, 300],
      // zIndex: 0,
      // border: [10, 'blue'],
      // gradients: {
      //   bgcolor: {
      //     // direction: 90,
      //     // rect: [0, 0, 100, 100],
      //     vector: [50, 50, 10, 70, 80, 100],
      //     colors: [{
      //       offset: 0,
      //       color: 'rgba(255,0,0,0.5)',
      //     }, {
      //       offset: 1,
      //       color: 'rgba(255,0,0,1)',
      //     }],
      //   },
      // },
    });
    layer.append(s);
    // layer.draw()
    
    const s2 = new Sprite({
      anchor: 0.5,
      border: [10, 'blue'],
      bgcolor: 'green',
      size: [50, 50],
      pos: [60, 100],
    });
    // const animation = s2.animate([
    //   {pos: [100, 100]},
    //   {pos: [100, 300]},
    //   {pos: [300, 300]},
    //   {pos: [300, 100]},
    //   {pos: [100, 100]},
    // ], {
    //   duration: 2000,
    //   iterations: Infinity,
    // })
    s.append(s2);
    // window.anim = animation
    // layer.draw()

    // s2.on('click', (evt) => {
    //   console.log(evt);
    //   evt.stopPropagation();
    // });

    s2.animate([{
      borderWidth: 1,
    }], {
      duration: 2000,
    });

    s.on('click', (evt) => {
      console.log(evt, evt.targetSprites);
    });

    // s.on('mouseleave', (evt) => {
    //   console.log(evt);
    // });
    layer.on('mouseenter', (evt) => {
      console.log(evt.type);
    });

    mycanvas.addEventListener('click', (evt) => {
      const {offsetX, offsetY} = evt;
      layer.dispatchEvent('click', {layerX: offsetX, layerY: offsetY, originEvent: evt});
    });

    mycanvas.addEventListener('mousemove', (evt) => {
      const {offsetX, offsetY} = evt;
      layer.dispatchEvent('mousemove', {layerX: offsetX, layerY: offsetY, originEvent: evt});
    });
    mycanvas.addEventListener('mousedown', (evt) => {
      const {offsetX, offsetY} = evt;
      layer.dispatchEvent('mousedown', {layerX: offsetX, layerY: offsetY, originEvent: evt});
    });
    mycanvas.addEventListener('mouseup', (evt) => {
      const {offsetX, offsetY} = evt;
      layer.dispatchEvent('mouseup', {layerX: offsetX, layerY: offsetY, originEvent: evt});
    });
  </script>
</body>
</html>